package com.tledu.production.controller.device;


import cn.hutool.json.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tledu.production.core.result.AjaxResult;
import com.tledu.production.model.device.DeviceFault;
import com.tledu.production.model.device.DeviceType;
import com.tledu.production.service.device.DeviceFaultService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/device")
@AllArgsConstructor
public class DeviceFaultController {

    private final DeviceFaultService deviceFaultService;


    //分页
    @GetMapping("/device/fault/list")
    @ResponseBody
    public AjaxResult<PageInfo<DeviceFault>> DeviceFaultList(Integer pageNum, Integer pageSize){
        if (pageNum == null) {pageNum = 1;}
        if (pageSize == null) {pageSize = 5;}
        // 下次请求会带上分页数据
        PageHelper.startPage(pageNum, pageSize);
        List<DeviceFault> list = deviceFaultService.list();
        // 通过这个api获取pageInfo
        PageInfo<DeviceFault> page = PageInfo.of(list);

//        throw new CustomException("测试页面报错");
        return new AjaxResult<>(page);
    }


    //添加
    @PostMapping("/device/fault/add")
    @ResponseBody
    public AjaxResult<Integer> DeviceFaultAdd(DeviceFault deviceFault) {

        return new AjaxResult<>(deviceFaultService.insert(deviceFault));
    }




    //编辑
    @PostMapping("/device/fault/update")
    @ResponseBody
    public AjaxResult<Integer> DeviceFaultUpdate(DeviceFault deviceFault) {

        return new AjaxResult<>(deviceFaultService.update(deviceFault));
    }

    //删除
    @GetMapping("/device/fault/delete")
    @ResponseBody
    public JSONObject delete(String deviceFaultId) {
        int delete = deviceFaultService.delete(deviceFaultId);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("success", delete);
        return jsonObject;
    }



    //按不同字段搜索
    @GetMapping("/device/fault/search")
    @ResponseBody
    public AjaxResult<PageInfo<DeviceFault>> DeviceFaultSearchList(Integer pageNum, Integer pageSize, String type, String searchText) {
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 5;
        }
        // 下次请求会带上分页数据
        PageHelper.startPage(pageNum, pageSize);
        List<DeviceFault> list = deviceFaultService.search(type, searchText);
        // 通过这个api获取pageInfo
        PageInfo<DeviceFault> page = PageInfo.of(list);
        return new AjaxResult<>(page);
    }


    //富文本默认值
    @RequestMapping("/device/fault/editText/{deviceFaultId}")
    public String edit(@PathVariable String deviceFaultId, Model model) {
        String text = deviceFaultService.findEdit(deviceFaultId);
        model.addAttribute("editText",text);
        return "device/fault-rich-text";


    }

    //富文本保存
    @RequestMapping("/device/fault/edsave")
    @ResponseBody
    public AjaxResult<Void> edsave(String deviceFaultId,String deviceFaultDetail){
        DeviceFault deviceFault = deviceFaultService.findById(deviceFaultId);
        deviceFault.setDeviceFaultDetail(deviceFaultDetail);
        deviceFaultService.update(deviceFault);
        return new AjaxResult();
    }


    //批量删除
    @GetMapping("/device/fault/deleteAll")
    @ResponseBody
    public AjaxResult DeviceFaultDeleteAll(String ids){
        String[] idsarr = ids.split(",");
        int i =0;
        for(String s : idsarr){
            deviceFaultService.delete(s);
            i++;
        }
        if (i >= 1) {
            return new AjaxResult("删除成功");
        } else {
            return new AjaxResult("删除失败", 500);
        }
    }
}
